package com.taxicall.udptransport;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

//import com.sun.xml.internal.ws.encoding.soap.SerializationException;

public class SerializationUtils {

	public static byte[] serialize(Serializable s) {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		ObjectOutputStream oo = null;

		try {
			oo = new ObjectOutputStream(byteStream);
			oo.writeObject(s);

		} catch (IOException e) {
			throw new RuntimeException(e);
			
		} finally {
			if (oo != null)
				try {
					oo.close();
				} catch (IOException e) {
				}
		}
		
		return byteStream.toByteArray();
	}

	public static Object deserialize(byte[] bytes) {
		ObjectInputStream ois = null;
		Object o;

		try {
			ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
			o = ois.readObject();

		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
			
		} finally {
			try {
				if (ois != null)
					ois.close();
			} catch (IOException e) {
			}
		}

		return o;
	}
}
